package Classes.GameDatas// ActionScript file
{
	import Classes.GameRecords.GR_Build;
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Func;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapTarget;
	import Classes.Managers.M_ConfigData;
	
	import flash.geom.Point;

	/**
	 *建筑数据类 
	 * @author 葛增文
	 * 
	 */	
	public class GD_Building_Func extends GD_Building
	{
		private static var _buildingsConfigObj:Object;
		private static var _tmpIDCount:Number=0;
		
		private var _buildingConfigObj:Object;
		private var _map:GD_Map;/*所属地图*/
		private var _buildingType:Number;/*建筑类型*/
		private var _serverID:Number;/*服务器ID*/
		private var _buildingLv:int;/*建筑等级*/
		private var _coordinate:Number;/*建筑坐标值*/
		private var _view:MT_Building_Func;/*建筑显示对象*/
		private var _buildType:BuildType;/*建设类型对象*/
		private var _isBar:Boolean;/*是否有阻挡*/
		private var _tmpID:Number;/*临时ID*/
		/**
		 * 判断地图上面是否已经摆放了此建筑 
		 */	
		private var _tmpHasBuildInMap:Boolean;
		
		/**
		 * 商品流水号 
		 */		
		public var shopId:Number;
		/**
		 * 商品价格
		 */	
		public var bulidPrice:Number;
		/**
		 * 商品货币类型  (0 表示欧币 1 表示金币 2跟（itemid）表示中间物品)
		 */	
		public var moneyType:Number;
		
		public function GD_Building_Func(buildingType:Number):void
		{
			this._buildingType=buildingType;
			this.init();
		}
		
		private function init():void
		{
			this._buildingConfigObj=buildingsConfigObj[buildingType];
		}
		
		/**
		 *创建一个建筑对象 
		 * @param creatObj 创建数据对象
		 * @return 建筑对象
		 * 
		 */		
		public static function creat(creatObj:Object,map:GD_Map=null):GD_Building_Func
		{
			var creatTarget:GD_Building_Func=new GD_Building_Func(creatObj.buildingType);
			creatTarget._map=map;
			creatTarget._buildingLv=creatObj.buildingLv;
			if(creatObj.buildingID)creatTarget._serverID=creatObj.buildingID;
			if(creatObj.tmp)creatTarget.tmpID=getTmpID();
			if(creatObj.coordinate)creatTarget._coordinate=creatObj.coordinate;
			return creatTarget;
		}
		
		/**
		 *恢复建筑的变化 
		 * @param changeType
		 * 
		 */		
		public function recoverChange():void
		{
			switch(this.buildType.changeType)
			{
				case BuildType.BUY:
					this.recoverBuy();
					break;
				case BuildType.PLACE:
					this.recoverPlace();
					break;
				case BuildType.MOVE:
					this.recoverMove();
					break;
				case BuildType.DELETE:
					this.recoverDelete();
					break;
			}
		}
		
		/**
		 *恢复购买变化 
		 * 
		 */		
		public function recoverBuy():void
		{
			if(this.moneyType==2)
			{
				GD_Player.instance.exchangeBuildItemNum+=this.bulidPrice;
			}else if(this.moneyType==1){
				GD_Player.instance.money+=this.bulidPrice;
			}else if(this.moneyType==0){
				GD_Player.instance.gold+=this.bulidPrice;
			}else{
				GD_Player.instance.exchangeBuildItemNum+=this.bulidPrice;
			}
			this.map.removeBuildingByID(this.serverID);
			this.release();
		}
		
		/**
		 *恢复放置变化 
		 * 
		 */		
		public function recoverPlace():void
		{
			var buildingItem:GD_BuildingItem=GD_Player.instance.getBuildingItemByType(this.buildingItemType);
			var changedNum:int;
			if(buildingItem)
			{
				changedNum=buildingItem.buildingItemNum+1;
			}
			else
			{
				changedNum=1;
			}
			GD_Player.instance.storeBuildingNumChanged({buildingItemType:this.buildingItemType,value:changedNum});
			this.map.removeBuildingByID(this.serverID);
			this.release();
		}
		
		/**
		 *恢复移动变化 
		 * 
		 */		
		private function recoverMove():void
		{
			var initGrid:GD_MapGrid=this.buildType.initGrid;
			if(initGrid)GV_Map.instance.addTarget(this.view,initGrid);
		}
		
		/**
		 *恢复删除变化 
		 * 
		 */		
		private function recoverDelete():void
		{
			var buildingItem:GD_BuildingItem=GD_Player.instance.getBuildingItemByType(this.buildingItemType);
			GD_Player.instance.storeBuildingNumChanged({buildingItemType:this.buildingItemType,value:buildingItem.buildingItemNum-1});
			var initGrid:GD_MapGrid=this.buildType.initGrid;
			if(initGrid)GV_Map.instance.addTarget(this.view,initGrid);
		}
		
		/**
		 *更新坐标值 
		 * 
		 */		
		override public function updateCoordinate():void
		{
			if(this.view.mapGrid)
			{
			    this._coordinate=this.view.mapGrid.coordinate;
			}
		}
		
		/**
		 *设定建设类型 
		 * @param type
		 * 
		 */		
		public function setBuildType(type:String=null):void
		{
			if(!this._buildType)
			{
				this._buildType=BuildType.creat({building:this});
				GR_Build.instance.addBuilding(this);
			}
			if(type)
			{
			    this.buildType.setType(type);
			}
		}
		
		/**
		 *清除建设类型 
		 * 
		 */		
		public function cleanBuildType():void
		{
			this._buildType=null;
		}
		
		/**
		 *获取临时ID 
		 * @return 
		 * 
		 */		
		public static function getTmpID():Number
		{
			_tmpIDCount-=1;
			return _tmpIDCount;
		}
		
		/**
		 *设置建筑 
		 * 
		 */		
		public function beSetting():void
		{
			this.setBuildType();
			this.view.removeFromMap();
			this.setBuildType(BuildType.MOVE);
			GV_Map.instance.layerBuildingSet.getBuilding(this.view);
		}
		
		/**
		 *清除建筑 
		 * 
		 */		
		public function beCleaning():void
		{
			if(this.map.mainBuilding==this)return;
			this.setBuildType(BuildType.DELETE);
			this.view.removeFromMap();
		}
		
		/**
		 *释放 
		 * 
		 */		
		override public function release():void
		{
			super.release();
			if(this._view)
			{
				//this._view.removeSign=1;
				if(this._view.mapGrid)
				this._view.mapGrid.release();
				this.view.mapGrid=null;
				this._view.release();
				this._view=null;
			}
		}

		/*getter & setter*/
		public function get buildingType():Number
		{
			return _buildingType;
		}

		/*public function get configXML():XML
		{
			return _configXML;
		}*/

		public function get buildingLv():int
		{
			return _buildingLv;
		}

		public function set buildingLv(value:int):void
		{
			_buildingLv = value;
		}

		public function get positionPoint():Point
		{
			var atx:int=this.coordinate%this.map.widthNum;
			var aty:int=int(this.coordinate/this.map.widthNum);
			return new Point(atx,aty);
		}
		
		override public function get size():int
		{
			return this.buildingConfigObj.size;
		}

		public function get view():MT_Building_Func
		{
			if(!_view)_view=MT_Building_Func.creat(this);
			return _view;
		}

		public function get map():GD_Map
		{
			return _map;
		}

		public function get buildType():BuildType
		{
			return _buildType;
		}

		public function get isBar():Boolean
		{
			return _isBar;
		}

		public function get serverID():Number
		{
			if(_serverID)
			{
				return _serverID;
			}
			else
			{
				return this.tmpID;
			}
		}

		override public function get coordinate():Number
		{
			return _coordinate;
		}
		
		public static function get buildingsConfigObj():Object
		{
			if(!_buildingsConfigObj)
			{
				_buildingsConfigObj=new Object;
				var buildingConfigDataCSV:String=M_ConfigData.instance.getCSVByName("功能建筑配置表");
				var buildingConfigDataAry:Array=GU_DataTransform.csvTransformObjsAry(buildingConfigDataCSV,16);
				for(var i:int=0;i<buildingConfigDataAry.length;i++)
				{
					var buildingConfigDataObj:Object=buildingConfigDataAry[i];
					if(!_buildingsConfigObj.hasOwnProperty(buildingConfigDataObj.zt_func_id))
					{
						_buildingsConfigObj[buildingConfigDataObj.zt_func_id]=new Object;
						_buildingsConfigObj[buildingConfigDataObj.zt_func_id].size=buildingConfigDataObj.zt_func_size_num;
						_buildingsConfigObj[buildingConfigDataObj.zt_func_id].type=buildingConfigDataObj.zt_func_type;
						_buildingsConfigObj[buildingConfigDataObj.zt_func_id].lvData=new Object;
					}
					_buildingsConfigObj[buildingConfigDataObj.zt_func_id].lvData[buildingConfigDataObj.zt_func_greade]=
						{
							name:buildingConfigDataObj.zt_func_name,
								buildingItemType:buildingConfigDataObj.zt_id,
								description:buildingConfigDataObj.zt_func_desc,
								itemPicSource:buildingConfigDataObj.zt_item_source,
								itemPic:buildingConfigDataObj.zt_item_pic,
								buildingPicSource:buildingConfigDataObj.zt_func_source,
								buildingPic:buildingConfigDataObj.zt_func_image,
								requireLv:buildingConfigDataObj.zt_func_user_greade,
								power:buildingConfigDataObj.zt_func_power
						};
				}
			}
			return _buildingsConfigObj;
		}
		
		public function get itemType():int
		{
			return 0;
		}
		
		public function get buildingItemType():Number
		{
			return this.buildingConfigObj.lvData[this.buildingLv].buildingItemType;
		}
		
		override public function get buildingPicSourceStr():String
		{
			return this.buildingConfigObj.lvData[this.buildingLv].buildingPicSource;
		}
		
		override public function get buildingPicStr():String
		{
			return this.buildingConfigObj.lvData[this.buildingLv].buildingPic
		}

		public function get buildingConfigObj():Object
		{
			return _buildingConfigObj;
		}

		public function get tmpID():Number
		{
			return _tmpID;
		}

		public function set serverID(value:Number):void
		{
			_serverID = value;
		}

		public function set tmpID(value:Number):void
		{
			_tmpID = value;
		}

		public static function get tmpIDCount():Number
		{
			return _tmpIDCount;
		}
		
		public function get ztFunType():Number
		{
			return Number(this.buildingConfigObj.type);
		}
		
		/**
		 * 判断地图上面是否已经摆放了此建筑 
		 */
		public function get tmpHasBuildInMap():Boolean
		{
			return _tmpHasBuildInMap;
		}
		
		/**
		 * @private
		 */
		public function set tmpHasBuildInMap(value:Boolean):void
		{
			_tmpHasBuildInMap = value;
		}
		
		//****************热感的属性******************
		
		override public function get gvName():String
		{
			return this.buildingConfigObj.lvData[this.buildingLv].name;
		}


	}
}